package com.szcinda.controller;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.metadata.Sheet;
import com.szcinda.controller.configuration.UserLoginToken;
import com.szcinda.repository.*;
import com.szcinda.service.PageResult;
import com.szcinda.service.dto.carrier.CarrierVehicleFeeExcelListener;
import com.szcinda.service.dto.carrier.CarrierVehicleFeeImportDto;
import com.szcinda.service.dto.client.*;
import com.szcinda.service.dto.line.LineQueryDto;
import com.szcinda.service.dto.line.VehicleFeeQueryDto;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@RestController
@RequestMapping("client")
public class ClientController {

    private final ClientService clientService;
    private final BaoGuanFeeRepository baoGuanFeeRepository;

    private final ClientVehiclePriceRepository clientVehiclePriceRepository;

    public ClientController(ClientService clientService, BaoGuanFeeRepository baoGuanFeeRepository, ClientVehiclePriceRepository clientVehiclePriceRepository) {
        this.clientService = clientService;
        this.baoGuanFeeRepository = baoGuanFeeRepository;
        this.clientVehiclePriceRepository = clientVehiclePriceRepository;
    }

    @UserLoginToken
    @PostMapping("create")
    @OperationLogAnnotation(operDesc = "创建客户", operModel = "客户管理")
    public Result<String> create(@RequestBody ClientCreateDto clientCreateDto) {
        clientService.create(clientCreateDto);
        return Result.success();
    }

    @UserLoginToken
    @PostMapping("modify")
    @OperationLogAnnotation(operDesc = "修改客户", operModel = "客户管理")
    public Result<String> update(@RequestBody ClientUpdateDto updateDto) {
        clientService.update(updateDto);
        return Result.success();
    }

    @GetMapping("queryByOrgId/{orgId}")
    public List<ClientDto> queryByOrgId(@PathVariable String orgId) {
        return clientService.getClientListByOrgId(orgId);
    }

    @PostMapping("query")
    public PageResult<ClientDto> query(@RequestBody ClientQueryDto queryDto) {
        return clientService.query(queryDto);
    }

    @GetMapping("del/{id}/{userId}")
    @OperationLogAnnotation(operDesc = "删除客户", operModel = "客户管理")
    public Result<String> del(@PathVariable String id, @PathVariable String userId) {
        clientService.delete(id, userId);
        return Result.success();
    }

    @GetMapping("delVehiclePrice/{id}")
    @OperationLogAnnotation(operDesc = "删除车型费用", operModel = "客户或者供应商管理")
    public Result<String> delVehiclePrice(@PathVariable String id) {
        clientVehiclePriceRepository.delete(id);
        return Result.success();
    }

    @GetMapping("getLines/{clientId}")
    public Result<List<LinePrice>> getLines(@PathVariable String clientId) {
        return Result.success(clientService.getLinesByClientId(clientId, false));
    }

    // 只过滤综合物流的线路
    @GetMapping("getLines2/{clientId}")
    public Result<List<LinePrice>> getLines2(@PathVariable String clientId) {
        return Result.success(clientService.getLinesByClientId(clientId, true));
    }

    @PostMapping("getLines")
    public Result<List<LinePrice>> getLines(@RequestBody LineQueryDto lineQueryDto) {
        return Result.success(clientService.getLines(lineQueryDto));
    }

    @GetMapping("{id}")
    public Result<ClientDetailDto> GetById(@PathVariable String id) {
        return Result.success(clientService.getById(id));
    }

    @GetMapping("findFeesByClientId/{clientId}")
    public Result<List<OrderFee>> findFeesByClientId(@PathVariable String clientId) {
        return Result.success(clientService.getByClientId(clientId));
    }

    @GetMapping("all")
    public Result<List<ClientDto>> all() {
        return Result.success(clientService.getAll());
    }

    @GetMapping("baoGuanFee/{clientId}")
    public Result<BaoGuanFee> baoGuanFee(@PathVariable String clientId) {
        return Result.success(baoGuanFeeRepository.findByBelongId(clientId));
    }

    @PostMapping("vehicleFees")
    public Result<List<ClientVehiclePriceDto>> vehicleFees(@RequestBody VehicleFeeQueryDto vehicleFeeQueryDto) {
        return Result.success(clientService.getVehiclePrices(vehicleFeeQueryDto));
    }

    @PostMapping("/import/{opId}")
    @OperationLogAnnotation(operDesc = "导入客户", operModel = "客户管理")
    public Result<String> orderImport(@RequestParam("file") MultipartFile file, @PathVariable String opId) throws Exception {
        // 解析每行结果在listener中处理
        InputStream inputStream = file.getInputStream();
        try {
            ClientExcelListener listener = new ClientExcelListener();
            EasyExcelFactory.readBySax(inputStream, new Sheet(1, 1, ClientImportDto.class), listener);
            clientService.batchCreate(listener.getImportDatas(), opId);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @PostMapping("/importVehicle/{belongId}")
    @OperationLogAnnotation(operDesc = "导入客户车型费用", operModel = "客户管理")
    public Result<String> importVehicle(@RequestParam("file") MultipartFile file, @PathVariable String belongId) throws Exception {
        // 解析每行结果在listener中处理
        InputStream inputStream = file.getInputStream();
        try {
            CarrierVehicleFeeExcelListener listener = new CarrierVehicleFeeExcelListener();
            EasyExcelFactory.readBySax(inputStream, new Sheet(1, 1, CarrierVehicleFeeImportDto.class), listener);
            clientService.batchCreateVehicleList(listener.getImportDatas(), belongId);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
